<#include "../include/header.html">
<script type="text/javascript">
    var viewModel = Hap.createGridViewModel('#grid',{
    });
</script>
<body>
<div id="page-content">
    <div class="pull-right" id="query-form" style="padding-bottom:10px;">
            <input type="text"   style="float:left;width:150px;margin-right:5px;" placeholder='<@spring.message "messageemailconfig.accountcode"/>' data-bind="value:model.messageFrom" class="k-textbox">
            <input type="text"  id="status-combobox"  style="width:150px;margin-right:5px;"  placeholder='<@spring.message "message.sendflag"/>' data-bind="value:model.sendFlag"/>
            <input type="text"   style="float:left;width:150px;margin-right:5px;" placeholder='<@spring.message "message.subject"/>' data-bind="value:model.subject" class="k-textbox">
        <span class="btn btn-primary"  style="margin-right:5px;" data-bind="click:query" type="submit"><i class="fa fa-search" style="margin-right:3px;"></i><@spring.message "hap.query"/></span>
        <div style="clear:both"></div>
    </div>
    <script>
        $("#status-combobox").kendoComboBox({
            dataTextField: "text",
            dataValueField: "value",
            valuePrimitive:true,
            dataSource: [
                { text: '<@spring.message "hap.failure"/>', value: "F" },
                { text: '<@spring.message "hap.success"/>', value: "Y" },
                { text: '<@spring.message "message.sending"/>', value: "P" },
                { text: '<@spring.message "message.unsent"/>', value: "N" }
            ]
        });

        kendo.bind($('#query-form'), viewModel);</script>
    <div style="clear:both">
        <div id="grid"></div>
    </div>
</div>

<div id="errorMessage"/>
<div id="receiver"/>
<script type="text/javascript">
    Hap.initEnterQuery('#query-form', viewModel.query);
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "${base.contextPath}/sys/message/query",
                dataType: "json"
            },
            parameterMap: function(options, type) {
                if (type !== "read" && options.models) {
                    var datas = Hap.prepareSubmitParameter(options, type);
                    return kendo.stringify(datas);
                } else if (type === "read") {
                    return Hap.prepareQueryParameter(viewModel.model.toJSON(), options);
                }
            }
        },
        batch: true,
        serverPaging: true,
        serverSorting: true,
        pageSize: 10,
        schema: {
            data: 'rows',
            total: 'total',
            model: {
                id: "messageId",
            }
        }
    });

    var grid = $("#grid").kendoGrid({
        dataSource: dataSource,
        navigatable: true,
        dataBound: function () {
            if(parent.autoResizeIframe){
                parent.autoResizeIframe('${RequestParameters.functionCode!}')
            }
        },
        resizable: true,
        sortable: true,
        pageable: {
            pageSizes: [5, 10, 20, 50],
            refresh: true,
            buttonCount: 5
        },
        columns: [{
                field: "messageFrom",
                title: '<@spring.message "messageemailconfig.accountcode"/>',
                width: 120
            }, {
                field: "subject",
                title: '<@spring.message "message.subject"/>',
                width: 180
            },  {
                field: "creationDate",
                title: '<@spring.message "messagetemplate.creationdate"/>',
                width: 180,
            	attributes: {
                    style: "text-align:center"
                },
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                }
            }, {
                field: "lastUpdateDate",
                title: '<@spring.message "messagetemplate.lastupdatedate"/>',
                width: 180,
            	attributes: {style: "text-align:center"},
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                }
            }, {
                field: "sendFlag",
                title: '<@spring.message "message.sendflag"/>',
                width: 120,
                template:function(dataItem){
                    if(dataItem.sendFlag=='F'){
                        return '<span style="color: #c9302c;"><@spring.message "hap.failure"/></span>'
                    }else if(dataItem.sendFlag=='N'){
                        return '<span><@spring.message "message.unsent"/></span>'
                    }else if(dataItem.sendFlag=='P'){
                        return  '<span><@spring.message "message.sending"/></span>'
                    }else if(dataItem.sendFlag=='Y'){
                        return '<span><@spring.message "hap.success"/></span>'
                    }
                },
                attributes: {style: "text-align:center"},
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                }
            },{
                field: "content",
                title: '<@spring.message "message.content"/>',
                width: 100,
                sortable: false,
                attributes: {
                    style: "text-align:center"
                },
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                },
                template: function(dataItem) {
                    return "<a href='javascript:void(0)' onclick=\'showMessageContent("+dataItem.messageId+")\'><@spring.message 'hap.view'/></a>";
                }
            },  {
                title: '<@spring.message "message.receivers"/>',
                width: 100,
                attributes: {
                    style: "text-align:center"
                },
                headerAttributes: {
                    "class": "table-header-cell",
                    style: "text-align: center"
                },
                template: function(dataItem){
                    return '<a href="javascript:void(0)" onclick=showReceivers(\"'+dataItem.uid+'\")><@spring.message "hap.view"/></a>';
                }
            },{
                 title:'<@spring.message "hap.error"/>',
                 width: 100,
                 sortable: false,
                 attributes: {
                     style: "text-align:center"
                 },
                 headerAttributes: {
                     "class": "table-header-cell",
                     style: "text-align: center"
                 },
                 template: function(dataItem) {
                    if(dataItem.sendFlag=='F'){
                        return "<a href='javascript:void(0)' onclick=\'showErrorMessage("+dataItem.messageId+")\'><@spring.message 'hap.view'/></a>";
                    }else{
                        return "";
                    }
            }
            }
        ],
        editable: false
    }).data("kendoGrid");

    function showReceivers(uid){
        var data = dataSource.getByUid(uid);
        var receiver = $("#receiver").kendoWindow({
            title: '<@spring.message "message.receivers"/>',
            width: 600,
            height: 300,
            scrollable: false,
            iframe:true,
            modal: true,
            content:"message_status_receiver.html?messageId="+data.messageId
        }).data("kendoWindow");
        receiver.center().open();
    }

    function showMessageContent(id) {
        $.ajax({
            type: "POST",
            url: "${base.contextPath}/sys/message/messageContent?messageId=" + id,
            success:function(data){
                kendo.ui.showDialog({
                    title: '<@spring.message "message.content"/>',
                    width: 600,
                    height: 300,
                    message: data,
                    buttons: [{
                        text: "<@spring.message 'hap.ok'/>",
                        type: 'primary',
                        click: function(e) {
                            e.dialog.destroy();
                            e.deferred.resolve({
                                button: "BUTTON1"
                            });
                        }
                    }]
                });

            }
        });

    }

    function showErrorMessage(id){
        $.ajax({
            type: "GET",
            url: "${base.contextPath}/sys/message/error_mess?messageId=" + id,
            success:function(data){
                kendo.ui.showDialog({
                    title: '<@spring.message "message.errormessage"/>',
                    width: 800,
                    height: 300,
                    message: data,
                    buttons: [{
                        text: "<@spring.message 'hap.ok'/>",
                        type: 'primary',
                        click: function(e) {
                            e.dialog.destroy();
                            e.deferred.resolve({
                                button: "BUTTON1"
                            });
                        }
                    }]
                });

            }
        });
    }

</script>
</body>
</html>